package bocchi.card;

import bocchi.base.BaseCustomCard;
import com.badlogic.gdx.graphics.Color;
import com.megacrit.cardcrawl.cards.AbstractCard;
import com.megacrit.cardcrawl.characters.AbstractPlayer;
import com.megacrit.cardcrawl.core.CardCrawlGame;
import com.megacrit.cardcrawl.dungeons.AbstractDungeon;
import com.megacrit.cardcrawl.monsters.AbstractMonster;
import com.megacrit.cardcrawl.vfx.BorderFlashEffect;
import com.megacrit.cardcrawl.vfx.combat.TimeWarpTurnEndEffect;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class GoBack extends BaseCustomCard {
    private static final String RAW_ID = GoBack.class.getSimpleName();
    public static final String GAME_ID = RAW_ID;

    private int playerHP;
    private int playerBlock;
    private Map<AbstractMonster, Integer> enemyHPs;
    private Map<AbstractMonster, Integer> enemyBlocks;

    public GoBack() {
        super(RAW_ID);
    }

    public GoBack(AbstractPlayer player, List<AbstractMonster> monsters) {
        super(RAW_ID);

        playerHP = player.currentHealth;
        playerBlock = player.currentBlock;
        enemyHPs = new HashMap<>();
        enemyBlocks = new HashMap<>();
        for (AbstractMonster monster : monsters) {
            enemyHPs.put(monster, monster.currentHealth);
            enemyBlocks.put(monster, monster.currentBlock);
        }
    }

    @Override
    public void use(AbstractPlayer player, AbstractMonster monster) {
        if (enemyHPs == null || enemyBlocks == null) {
            System.out.println("GoBack: enemyHPs or enemyBlocks is null");
            return;
        }

        CardCrawlGame.sound.play("POWER_TIME_WARP", 0.05F);
        AbstractDungeon.effectsQueue.add(new BorderFlashEffect(Color.GOLD, true));
        AbstractDungeon.topLevelEffectsQueue.add(new TimeWarpTurnEndEffect());

        player.currentHealth = playerHP;
        player.currentBlock = playerBlock;
        for (Map.Entry<AbstractMonster, Integer> entry : enemyHPs.entrySet()) {
            entry.getKey().currentHealth = entry.getKey().currentHealth > 0 ? entry.getValue() : 0;
        }
        for (Map.Entry<AbstractMonster, Integer> entry : enemyBlocks.entrySet()) {
            entry.getKey().currentBlock = entry.getKey().currentHealth > 0 ? entry.getValue() : 0;
        }
    }

    @Override
    public AbstractCard makeStatEquivalentCopy() {
        GoBack card = (GoBack) super.makeStatEquivalentCopy();
        if (enemyHPs != null && enemyBlocks != null) {
            card.playerHP = playerHP;
            card.playerBlock = playerBlock;
            card.enemyHPs = new HashMap<>(enemyHPs);
            card.enemyBlocks = new HashMap<>(enemyBlocks);
        }
        return card;
    }
}
